﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SAKS.Models;
using SAKS.ViewModels;
using SAKS.IServices;
using SAKS.ActionFilters;

namespace SAKS.Controllers
{
    [NeedsPersistence]
    public class PersonController : ControllerBase
    {
        public IPersonServices PersonServices { get; set; }

        protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        {
            base.Initialize(requestContext);
            var session = GetCurrentSession(requestContext);
            PersonServices.Session = session;
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(DataTableParameter param)
        {
            int? _start = param.iDisplayStart;
            int? _pageSize = param.iDisplayLength;
            int first = _start ?? 0;
            int pageSize = _pageSize ?? 20;
            int? totalRecords = new int?();
            var ret = PersonServices.List(first, pageSize, out totalRecords, param.sSearch);
            var json = Json(
                new dataTableJson
                {
                    sEcho = param.sEcho,
                    iTotalRecords = totalRecords,
                    iTotalDisplayRecords = totalRecords,
                    aaData = ret
                },
                JsonRequestBehavior.AllowGet
            );
            return json;
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(PersonInfoCreateUpdateModel info)
        {
            var minfo = PersonInfoMapper.ToPersonInfo(info);
            PersonServices.Save(minfo);
            return RedirectToAction("index");
        }

        public ActionResult Edit(Guid id)
        {         
            var person = PersonServices.Get(id);
           
            if (person != null)
            {
                var model = PersonInfoMapper.ToCreateUpdateModel(person);
                return View(model);
            }
            else
                return HttpNotFound();
        }

        [HttpPost]
        public ActionResult Edit(PersonInfo info)
        {
            PersonServices.Update(info);
            return RedirectToAction("Index");
        }

        public ActionResult ajaxPersonInfoByRealName(string realname)
        {
            var info = PersonServices.GetPersonInfoByRealName(realname);
            if (info != null && info.Length > 0)
            {
                var result = new AjaxResponse { State = "Succeed", Data = info, Message = "" };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            else
                return Json(new AjaxResponse { State = "Failure", Message = "信息尚未录入" }, JsonRequestBehavior.AllowGet);
        
        }
    }
}
